﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using JasLib.Extensions;
using JasLib.Interop.Win32;
using Microsoft.Win32.SafeHandles;

namespace JasLib.Interop
{
    public static class InteropUtilities
    {
        /***************************************************************************/
        public unsafe static LPITEMIDLIST[] Split(CIDA* pcida)
        {
            LPITEMIDLIST[] itemIdLists = new LPITEMIDLIST[pcida->cidl];

            for (uint uiIndex = 0; uiIndex < pcida->cidl; uiIndex++)
            {
                itemIdLists[uiIndex] = SHELL32.ILCombine((byte*)pcida + pcida->aoffset[0], (byte*)pcida + pcida->aoffset[1 + uiIndex]);
            }

            return itemIdLists;
        }

        /***************************************************************************/
        public static IEnumerable<string> SplitDoubleNullTerminated(char[] buffer, int iLength = -1)
        {
            if (iLength == -1)
                iLength = buffer.Length;

            StringBuilder sb = new StringBuilder(iLength);
            int iIndex = 0;
            while (iIndex < iLength && buffer[iIndex] != 0)
            {
                sb.Length = 0;
                while (buffer[iIndex] != 0)
                {
                    sb.Append(buffer[iIndex]);
                    iIndex++;
                }
                yield return sb.ToString();
                iIndex++;
            }
        }

        /***************************************************************************/
        public static char[] JoinDoubleNullTerminated(this IEnumerable<string> strings)
        {
            if (strings == null)
                strings = Enumerable.Empty<string>();

            List<char[]> charArrays = new List<char[]>();
            int iFinalLength = 1;
            foreach (string str in strings)
            {
                char[] chars = str.ToCharArray();
                iFinalLength += (chars.Length + 1);
                charArrays.Add(chars);
            }

            char[] finalChars = new char[iFinalLength];
            int iWriteOffset = 0;
            foreach (char[] chars in charArrays)
            {
                Array.Copy(chars, 0, finalChars, iWriteOffset, chars.Length);
                iWriteOffset += chars.Length;
                finalChars[iWriteOffset++] = (char)0;
            }
            finalChars[iWriteOffset++] = (char)0;

            return finalChars;
        }

        /***************************************************************************/
        public static string JoinDoubleNullTerminated(params string[] strings)
        {
            return new string(JoinDoubleNullTerminated((IEnumerable<string>)strings));
        }

        /************************************************************************************/
        public static long UnixTimeToFileTimeTicks(long time_t)
        {
            return (116444736000000000 + (10000000 * time_t));
        }
    }
}
